# -*- coding: UTF-8 -*-
import os
import os.path
import sys

current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(current_dir)
sys.path.append("..")

import ruamel.yaml as yaml
import random
from misc import *
import operator


class cash(object):
    """description of class"""
    file_path_str = "filePath"
    currency_str = "currency"
    version_str = "version"
    value_str = "value"
    sn_str = "sn"
    error_code_str = "error_code"
    after_break_str = "after_break"
    adc_0_str = "ADC0FileName"
    adc_1_str = "ADC1FileName"
    cis_top_str = "topCISFileName"
    cis_bottom_str = "bottomCISFileName"
    time_str = "cost_time"

    dictionary_key = [time_str, file_path_str, currency_str, version_str, value_str, sn_str, error_code_str,
                      after_break_str]
    # 需要run比较的attr
    attribute_key = [currency_str, version_str, value_str, sn_str, error_code_str, after_break_str]
    not_run_attribute_key = [time_str]
    file_path_key = [adc_0_str, adc_1_str, cis_top_str, cis_bottom_str]

    def __init__(self, index=0, dict=None):
        assert isinstance(index, (int))
        self.index = index
        self.dictionary = {}
        # 1.3 wwm
        for key in self.not_run_attribute_key:
            self.dictionary[key] = "-"
        if dict is not None:
            # assert cmp(self.dictionary_key, dict.keys())
            if not operator.eq(self.dictionary_key, list(dict.keys())):
                raise YamlFormatError(" cash information data key error")
            # assert cmp(self.file_path_key, dict[self.file_path_str].keys())
            if not operator.eq(self.file_path_key, list(dict[self.file_path_str].keys())):
                raise YamlFormatError("cash file data key error")

            for key in self.attribute_key:
                self.dictionary[key] = dict[key]
                # print  self.dictionary[key]
            self.dictionary[self.file_path_str] = {}
            for key in self.file_path_key:
                self.dictionary[self.file_path_str][key] = dict[self.file_path_str][key]
            # print self.dictionary
        else:
            for key in self.dictionary_key:
                self.dictionary[key] = ""
            self.dictionary[self.file_path_str] = {}
            for key in self.file_path_key:
                self.dictionary[self.file_path_str][key] = ""
            self.dictionary[self.after_break_str] = False

    def setCashAttribute(self, key, value):
        assert key in self.dictionary
        self.dictionary[key] = value
        if key == cash.error_code_str and value:
            self.dictionary[key] = [int(value)]
        elif key == cash.error_code_str and not value:
            self.dictionary[key] = [0]

    def getCashAttribute(self, key):
        assert key in self.dictionary
        return self.dictionary[key]

    def setFileAttribute(self, key, value):
        assert key in self.dictionary[self.file_path_str]
        self.dictionary[self.file_path_str][key] = value

    def geFileAttribute(self, key):
        assert key in self.dictionary[self.file_path_str]
        return self.dictionary[self.file_path_str][key]

    def setIndex(self, index):
        self.index = index

    def getIndex(self):
        return self.index

    def __str__(self):
        return yaml.dump({self.index: self.dictionary}, Dumper=yaml.RoundTripDumper)

    def __repr__(self):
        return yaml.dump({self.index: self.dictionary}, Dumper=yaml.RoundTripDumper)

    # 模式匹配
    def CompareCash(self, run_cash, pattern, search_pattern):
        if (pattern == "0"):
            return self.CompareCash_positve(run_cash, search_pattern)
        elif (pattern == "1"):
            return self.CompareCash_negative(run_cash)
        elif (pattern == "2"):
            return self.CompareCash_fake(run_cash)
        elif (pattern == "3"):
            return self.CompareCash_sn(run_cash)

    # 1.判断属性值是否存在；都不存在->继续  只有模板存在存在->错误  只有run存在->正确  2.都存在判断是否相等
    def CompareCash_positve(self, run_cash, search_pattern):
        assert isinstance(run_cash, cash)
        ret_value = True
        for key in cash.attribute_key:
            # 转化为字符串对比  防止形式不一样出错
            a, b = self.getCashAttribute(key), run_cash.getCashAttribute(key)
            if key == self.sn_str and not a and search_pattern == "0":  # 如果模板中没有sn的值也算对不用判断
                continue
            if key == self.error_code_str and (a == [913] or b == [913]):
                continue
            # 先判断其是否存在
            if not a or not b:
                if not a and not b:
                    continue
                elif not b:
                    ret_value = False
                    break
                elif not a:
                    continue
            temp_a, temp_b = str(a), str(b)
            if temp_a != temp_b:
                ret_value = False
                break
        return ret_value

    def CompareCash_negative(self, run_cash):
        return self.CompareCash_positve(run_cash)
        # assert isinstance(run_cash,cash)
        # if run_cash.getCashAttribute(run_cash.version_str) and run_cash.getCashAttribute(run_cash.value_str):
        #     return False
        # if run_cash.getCashAttribute(run_cash.error_code_str)==[0]:
        #     return False
        # return True

    def CompareCash_fake(self, run_cash):  # 假币只有报错才返回正确
        assert isinstance(run_cash, cash)
        if run_cash.getCashAttribute(run_cash.error_code_str) == [0] or run_cash.getCashAttribute(
                run_cash.error_code_str) == [913]:
            return False
        return True

    def CompareCash_sn(self, run_cash):
        assert isinstance(run_cash, cash)
        a, b = self.getCashAttribute(run_cash.sn_str), run_cash.getCashAttribute(run_cash.sn_str)
        if not a:
            return True
        if not a or not b:
            if not b:
                return False
        if a != b:
            return False
        return True

# def CompareCash(cashl, cashr):
#     assert isinstance(cashl,cash)
#     assert isinstance(cashr,cash)
#     ret_value = True
#     for key in cash.attribute_key:
#
#         if cashl.getCashAttribute(key) != cashr.getCashAttribute(key) \
#             and cashl.getCashAttribute(key) != "any"\
#             and cashr.getCashAttribute(key) != "any":
#             ret_value = False
#             break

# a = random.randint(0,100)
# if a > 90:
#     ret_value = False
# else:
#     ret_value= True

# return ret_value
#
